package cn.xie.domain.strategy.service.rule.impl;

import cn.xie.domain.strategy.model.entity.RuleActionEntity;
import cn.xie.domain.strategy.model.entity.RuleFillerEntity;
import cn.xie.domain.strategy.model.entity.StrategyEntity;
import cn.xie.domain.strategy.model.entity.StrategyRuleEntity;
import cn.xie.domain.strategy.model.vo.RuleFillerCheckVO;
import cn.xie.domain.strategy.repository.IStrategyRepository;
import cn.xie.domain.strategy.service.annotation.LogicStrategy;
import cn.xie.domain.strategy.service.rule.IRuleFiller;
import cn.xie.domain.strategy.service.rule.factory.DefaultRuleFactory;
import cn.xie.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author: xiaoxie
 * create: 2025-08-08 17:46
 * @BelongsProject: bit-market
 * @BelongsPackage: cn.xie.domain.strategy.service.rule.impl
 * description: 黑名单过滤实现类
 */
@Slf4j
@Service
@LogicStrategy(ruleModel = DefaultRuleFactory.RuleModel.RULE_BLACK_LIST)
public class RuleBlackFillerImpl implements IRuleFiller<RuleActionEntity.RaffleRuleBeforeEntity> {

    @Resource
    private IStrategyRepository strategyRepository;

    @Override
    public RuleActionEntity<RuleActionEntity.RaffleRuleBeforeEntity> filler(RuleFillerEntity ruleFillerEntity) {
        log.info("规则过滤-黑名单 userId:{} strategyId:{} ruleModel:{}",
                ruleFillerEntity.getUserId(), ruleFillerEntity.getStrategyId(), ruleFillerEntity.getRuleModel());

        StrategyEntity strategyEntity = strategyRepository.queryStrategyByStrategyId(ruleFillerEntity.getStrategyId());
        String[] ruleModels = strategyEntity.getRuleModels();
        if (ruleModels == null || ruleModels.length == 0 ||
                !Arrays.asList(ruleModels).contains(Constants.StrategyRule.RULE_BLACK_LIST)) {
            // 没有黑名单规则，直接放行
            return allow();
        }

        StrategyRuleEntity strategyRuleEntity =
                strategyRepository.queryStrategyRule(ruleFillerEntity.getStrategyId(), ruleFillerEntity.getAwardId()
                        ,ruleFillerEntity.getRuleModel());

        Map<Integer, Set<String>> awardIdBlackUserIdMap = strategyRuleEntity.getAwardIdBlackUserIdMap();
        String userId = ruleFillerEntity.getUserId();

        // 遍历所有奖品，看当前用户是否命中黑名单
        for (Map.Entry<Integer, Set<String>> entry : awardIdBlackUserIdMap.entrySet()) {
            Integer awardId = entry.getKey();
            Set<String> blackUserIds = entry.getValue();
            if (blackUserIds != null && blackUserIds.contains(userId)) {
                return takeOver(ruleFillerEntity.getStrategyId(), awardId);
            }
        }

        // 全部奖品都没命中，放行
        return allow();
    }
    // 放行
    private RuleActionEntity<RuleActionEntity.RaffleRuleBeforeEntity> allow() {
        return RuleActionEntity.<RuleActionEntity.RaffleRuleBeforeEntity>builder()
                .code(RuleFillerCheckVO.ALLOW.getCode())
                .info(RuleFillerCheckVO.ALLOW.getInfo())
                .build();
    }
    // 接管
    private RuleActionEntity<RuleActionEntity.RaffleRuleBeforeEntity> takeOver(Long strategyId,Integer awardId) {
        return RuleActionEntity.<RuleActionEntity.RaffleRuleBeforeEntity>builder()
                .code(RuleFillerCheckVO.TAKE_OVER.getCode())
                .info(RuleFillerCheckVO.TAKE_OVER.getInfo())
                .ruleModel(DefaultRuleFactory.RuleModel.RULE_BLACK_LIST.getCode())
                .data(RuleActionEntity.RaffleRuleBeforeEntity.builder()
                        .strategyId(strategyId)
                        .awardId(awardId)
                        .build())
                .build();
    }

}
